package cc.eddic.pratice.programmingparadigms.nginxlog.impl;

import cc.eddic.pratice.programmingparadigms.nginxlog.LogItem;
import cc.eddic.pratice.programmingparadigms.nginxlog.LogItemParser;
import cc.eddic.pratice.programmingparadigms.nginxlog.NginxLogItem;
import lombok.extern.slf4j.Slf4j;


import java.time.LocalDateTime;
import java.util.Optional;


@Slf4j
public class LogItemParserCxmImpl implements LogItemParser {

    @Override
    public Optional<LogItem> parse(NginxLogItem nginxLogItem) {
        String[] request=nginxLogItem.getRequest().split(" ");
        String path="";
        if(request.length < 3){
            return Optional.empty();
        }else {
            String method=request[0];
            String url=request[1];
            String protocol=request[2];
            if(url.contains("?")){
                path=url.substring(0,request[1].indexOf("?"));
            }else{
                path=url;
            }
            return Optional.of(new LogItem(nginxLogItem.getIp(), LocalDateTime.now(),method,path,null,protocol,Integer.parseInt(nginxLogItem.getStatus()),Long.parseLong(nginxLogItem.getBytes()),nginxLogItem.getReferer(),nginxLogItem.getUserAgent()));

        }

        }




    }




